package com.nondo.dean.platform.controller.api.app;

import com.nondo.dean.common.annotation.ApiVersion;
import com.nondo.dean.common.web.Page;
import com.nondo.dean.common.web.Query;
import com.nondo.dean.common.web.View;
import com.nondo.dean.platform.domain.Customer;
import com.nondo.dean.platform.service.CustomerService;
import com.nondo.dean.platform.service.UserService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.List;

/**
 * @author xiaofan
 * @Description: 客户
 * @throws
 * @date 2018-05-1715:43
 * @Version 0.1
 */
@RestController("apiAppCustomer")
@RequestMapping("/{v}/app/customer")
public class CustomerController {

    @Autowired
    CustomerService customerService;
    
    @Autowired
    UserService userService;

    /**
     * 获取客源分页查询列表
     * @param request
     * @param response
     * @param query
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/list")
    public View<Page<Customer>> list(HttpServletRequest request, HttpServletResponse response, @RequestBody Query<Customer> query) {
        View<Page<Customer>> view = new View<Page<Customer>>(20000, "获取列表成功");

        Customer customer = query.getData();
        
        if(customer == null) {
            customer = new Customer();
        }
        customer.setUserId(userService.getLoginUserId());

        Page<Customer> page = new Page<Customer>(query.getPage(), query.getSize(), customerService.count(customer));

        List<Customer> customerList = customerService.getCustomerList(query.getOffset(), query.getLimit(), customer);

        page.setContent(customerList);

        view.setData(page);

        return view;
    }


    /**
     * 按用户名和联系方式查询列表
     * @param request
     * @param response
     * @param query
     * @return
     */
    @ApiVersion(1)
    @RequestMapping("/search/list")
    public View<Page<Customer>> searchlist(HttpServletRequest request, HttpServletResponse response, @RequestBody Query<Customer> query) {
        View<Page<Customer>> view = new View<Page<Customer>>(20000, "获取列表成功");

        Customer customer = query.getData();
        
        if(customer == null) {
            customer = new Customer();
        }
        customer.setUserId(userService.getLoginUserId());

        Page<Customer> page = new Page<Customer>(query.getPage(), query.getSize(), customerService.countSearch(customer));

        List<Customer> list = customerService.getSearchList(query.getOffset(), query.getLimit(), customer);

        page.setContent(list);

        view.setData(page);

        return view;
    }
}
